package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.service.CouponService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author XJX
 * @date 2021-12-9 21:11:19
 */

/**
 * @modifiedBy Jiazhe Yuan
 * @time 2021-12-23 11:30:37
 * @info 根据api增添查看购物券状态
 */

@RestController
@RefreshScope
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class CouponController {

    @Autowired
    CouponService couponService;

    /**
     * 买家查看优惠券列表
     *
     * @param state       优惠券状态
     * @param page        页
     * @param pageSize    页大小
     * @param loginUserId 用户id
     * @return 优惠券列表
     */
    @Audit
    @GetMapping("/coupons")
    public Object getCoupons(
            @RequestParam(required = false) Integer state,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @LoginUser Long loginUserId) {
        System.out.println("用户id" + loginUserId);
        return Common.decorateReturnObject(couponService.getCoupons(loginUserId, state, page, pageSize));
    }

    @GetMapping("/coupons/states")
    public Object getCouponStates() {
        ReturnObject<List> returnObject = couponService.getCouponStates();
        return Common.decorateReturnObject(returnObject);
    }

    /**
     * 顾客领取优惠券
     *
     * @param actId         活动id
     * @param loginUserId   用户id
     * @param loginUserName 用户名
     * @return 领取结果
     */
    @Audit
    @PostMapping("/couponactivities/{id}/coupons")
    public Object getCoupon(
            @PathVariable("id") Long actId,
            @LoginUser Long loginUserId,
            @LoginName String loginUserName) {
        System.out.println(loginUserId);
        return Common.decorateReturnObject(couponService.gainCoupon(actId, loginUserId, loginUserName));
    }
}